package com.huawei.esdk.tp.professional.local.impl.service;

import org.apache.log4j.Logger;

import com.huawei.esdk.tp.professional.local.bean.BroadInfoEx;
import com.huawei.esdk.tp.professional.local.bean.QueryBroadInfoEx;
import com.huawei.esdk.tp.professional.local.bean.QueryBroadInfoExResponse;
import com.huawei.esdk.tp.professional.local.bean.QueryConfCtrlPwdEx;
import com.huawei.esdk.tp.professional.local.bean.QueryConfCtrlPwdExResponse;
import com.huawei.esdk.tp.professional.local.bean.ReleaseChairEx;
import com.huawei.esdk.tp.professional.local.bean.RequestChairEx;
import com.huawei.esdk.tp.professional.local.bean.SetDirectBroadEx;
import com.huawei.esdk.tp.professional.local.bean.SetRecordBroadEx;
import com.huawei.esdk.tp.professional.local.bean.TPProfessionalTerminalConfCtr;
import com.huawei.esdk.tp.professional.local.bean.TPSDKResponseEx;
import com.huawei.esdk.tp.professional.local.impl.utils.ClientProvider;
import com.huawei.esdk.tp.professional.local.impl.utils.ExceptionUtils;
import com.huawei.esdk.tp.professional.local.terminal.TerminalServiceEx;

public class TerminalServiceExImpl implements TerminalServiceEx
{
    /**
     * 日志对象
     */
    private static final Logger LOGGER = Logger.getLogger(TerminalServiceExImpl.class);
    
    private TPProfessionalTerminalConfCtr tpProfessionalTerminalConfCtr =
        (TPProfessionalTerminalConfCtr)ClientProvider.getClient(TPProfessionalTerminalConfCtr.class);
    
    private static TerminalServiceExImpl instance = null;
    
    public static synchronized TerminalServiceExImpl getInstance()
    {
        if (null == instance)
        {
            instance = new TerminalServiceExImpl();
        }
        return instance;
    }
    
    public Integer requestChairEx(String siteURI)
    {
        LOGGER.info("requestChairEx method start");
        RequestChairEx requestChairEx = new RequestChairEx();
        requestChairEx.setSiteUri(siteURI);
        
        try
        {
            Integer errCode = tpProfessionalTerminalConfCtr.requestChairEx(requestChairEx).getResultCode();
            LOGGER.info("requestChairEx method end");
            return errCode;
        }
        catch (Exception e)
        {
            LOGGER.error("requestChairEx method error", e);
            return ExceptionUtils.processSoapException(e);
        }
    }
    
    public Integer releaseChairEx(String siteURI)
    {
        LOGGER.info("releaseChairEx method start");
        ReleaseChairEx releaseChairEx = new ReleaseChairEx();
        releaseChairEx.setSiteUri(siteURI);
        try
        {
            Integer errCode = tpProfessionalTerminalConfCtr.releaseChairEx(releaseChairEx).getResultCode();
            LOGGER.info("releaseChairEx method end");
            return errCode;
        }
        catch (Exception e)
        {
            LOGGER.error("releaseChairEx method error", e);
            return ExceptionUtils.processSoapException(e);
        }
    }
    
    public TPSDKResponseEx<Integer> queryConfCtrlPwdEx(String siteURI)
    {
        LOGGER.info("queryConfCtrlPwdEx method start");
        TPSDKResponseEx<Integer> resp = new TPSDKResponseEx<Integer>();
        QueryConfCtrlPwdEx queryConfCtrlPwdEx = new QueryConfCtrlPwdEx();
        queryConfCtrlPwdEx.setSiteUri(siteURI);
        QueryConfCtrlPwdExResponse queryConfCtrlPwdExResp;
        try
        {
            queryConfCtrlPwdExResp = tpProfessionalTerminalConfCtr.queryConfCtrlPwdEx(queryConfCtrlPwdEx);
        }
        catch (Exception e)
        {
            LOGGER.error("queryConfCtrlPwdEx method error", e);
            ExceptionUtils.processSoapException(resp, e);
            return resp;
        }
        Integer resultCode = queryConfCtrlPwdExResp.getResultCode();
        resp.setResult(0 == resultCode ? Integer.valueOf(queryConfCtrlPwdExResp.getResult()) : null);
        resp.setResultCode(resultCode);
        LOGGER.info("queryConfCtrlPwdEx method end");
        return resp;
    }
    
    public TPSDKResponseEx<BroadInfoEx> queryBroadInfoEx(String siteURI)
    {
        LOGGER.info("queryBroadInfoEx method start");
        TPSDKResponseEx<BroadInfoEx> resp = new TPSDKResponseEx<BroadInfoEx>();
        QueryBroadInfoEx queryBroadInfoEx = new QueryBroadInfoEx();
        queryBroadInfoEx.setSiteUri(siteURI);
        QueryBroadInfoExResponse queryBroadInfoExResp;
        try
        {
            queryBroadInfoExResp = tpProfessionalTerminalConfCtr.queryBroadInfoEx(queryBroadInfoEx);
        }
        catch (Exception e)
        {
            LOGGER.error("queryBroadInfoEx method error", e);
            ExceptionUtils.processSoapException(resp, e);
            return resp;
        }
        Integer resultCode = queryBroadInfoExResp.getResultCode();
        resp.setResult(0 == resultCode ? queryBroadInfoExResp.getBroadInfo() : null);
        resp.setResultCode(resultCode);
        LOGGER.info("queryBroadInfoEx method end");
        return resp;
    }
    
    public Integer setRecordBroadEx(String siteURI, Integer action)
    {
        LOGGER.info("setRecordBroadEx method start");
        SetRecordBroadEx setRecordBroadEx = new SetRecordBroadEx();
        setRecordBroadEx.setSiteUri(siteURI);
        setRecordBroadEx.setAction(action);
        try
        {
            Integer errCode = tpProfessionalTerminalConfCtr.setRecordBroadEx(setRecordBroadEx).getResultCode();
            LOGGER.info("setRecordBroadEx method end");
            return errCode;
        }
        catch (Exception e)
        {
            LOGGER.error("setRecordBroadEx method error", e);
            return ExceptionUtils.processSoapException(e);
        }
    }
    
    public Integer setDirectBroadEx(String siteURI, Integer action)
    {
        LOGGER.info("setDirectBroadEx method start");
        SetDirectBroadEx setDirectBroadEx = new SetDirectBroadEx();
        setDirectBroadEx.setSiteUri(siteURI);
        setDirectBroadEx.setAction(action);
        try
        {
            Integer errCode = tpProfessionalTerminalConfCtr.setDirectBroadEx(setDirectBroadEx).getResultCode();
            LOGGER.info("setDirectBroadEx method end");
            return errCode;
        }
        catch (Exception e)
        {
            LOGGER.error("setDirectBroadEx method error", e);
            return ExceptionUtils.processSoapException(e);
        }
    }
}
